package com.car.slave.im.model.user;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import android.content.ContentValues;
import android.database.Cursor;

import com.car.slave.im.model.IMMessage;

@SuppressWarnings("serial")
public abstract class IMUserMessage implements IMMessage, Serializable {
	
	public static final String COLUMN_UESER_ID = "userId";		
	public static final String COLUMN_MESSAGE_ID = "messageId";
	public static final String COLUMN_CREATE_TIME = "createTime";
	public static final String COLUMN_MESSAGE_TYPE = "messageType";				// 消息类型 0文字，1图片， 2声音，3客服欢迎语，4自动回复
	public static final String COLUMN_MESSAGE_DIRECTION = "messageDirction";	// 发送方向 0发送，1接受
	public static final String COLUMN_SEND_STATUS = "sendStatus";				// 发送状态 0发送中，1发送失败，2已发送
	public static final String COLUMN_READ_STATUS = "readStatus";				// 阅读状态 0未读，1已读

	public static final int TYPE_TEXT = 1;
	public static final int TYPE_VOICE = 2;
	public static final int TYPE_IMAGE = 3;

	public static final int DIRECTION_SEND = 0;
	public static final int DIRECTION_RECEIVED = 1;
	
	public static final int STATUS_SENDING = 0;
	public static final int STATUS_FAILURE = 1;
	public static final int STATUS_ARRIVED = 2;
	
	public String userId;
	public String messageId;
	public long createTime;
	
	/**
	 * 消息类型: 0文字，1图片， 2声音
	 */
	public int type;			
	
	/**
	 * 发送方向: 0发送，1接受
	 */
	public int direction;
	
	/**
	 * 发送状态: 0发送中，1发送失败，2已发送
	 */
	public int sentStatus;
	
	/**
	 * 阅读状态: 0未读，1已读
	 */
	private int readStatus;
	
	public IMUserMessage() {
		super();
	}

	/**
	 * @param messageId			消息id
	 * @param messageType		消息类型
	 * @param messageDirection	发送方向
	 */
	public IMUserMessage(String messageId, int messageType, int messageDirection) {
		super();
		this.createTime = System.currentTimeMillis();
		this.messageId = messageId;
		this.type = messageType;
		this.direction = messageDirection;
	}
	
	/**
	 * 标记为已读.
	 */
	public void markReadStatus() {
		this.readStatus = 1;
	}

	@Override
	public boolean equals(Object o) {
		if (!(o instanceof IMUserMessage)) {
			return false;
		}
		IMUserMessage dest = (IMUserMessage) o;
		return messageId.equals(dest.messageId) && userId.equals(dest.userId);
	}
	
	@Override
	public int hashCode() {
		return (messageId + userId).hashCode();
	}
	
	@Override
	public String toString() {
		return messageId;
	}
	
	/**
	 * 发送给服务端是个JSonObject,通过map转成json.
	 * @return
	 */
	public Map<String, String> getJsonMap() {
		Map<String, String> map = new HashMap<String, String>();
//		map.put(COLUMN_UESER_ID, userId);
		map.put("msg_id", messageId);
//		map.put(COLUMN_CREATE_TIME, String.valueOf(createTime));
		map.put("type", String.valueOf(type));
		return map;
	}
	
	/**
	 * Cursor转成实列.
	 * @return
	 */
	public void cursorToMessage(Cursor cursor) {
		try {
			userId = cursor.getString(cursor.getColumnIndex(COLUMN_UESER_ID));
			messageId = cursor.getString(cursor.getColumnIndex(COLUMN_MESSAGE_ID));
			createTime = cursor.getLong(cursor.getColumnIndex(COLUMN_CREATE_TIME));
			type = cursor.getInt(cursor.getColumnIndex(COLUMN_MESSAGE_TYPE));
			direction = cursor.getInt(cursor.getColumnIndex(COLUMN_MESSAGE_DIRECTION));
			sentStatus = cursor.getInt(cursor.getColumnIndex(COLUMN_SEND_STATUS));
			readStatus = cursor.getInt(cursor.getColumnIndex(COLUMN_READ_STATUS));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 数据库用到的ContentValues.
	 * @return
	 */
	public ContentValues getContentValues() {
		final ContentValues cv = new ContentValues();
		cv.put(COLUMN_UESER_ID, userId);
		cv.put(COLUMN_MESSAGE_ID, messageId);
		cv.put(COLUMN_CREATE_TIME, createTime);
		cv.put(COLUMN_MESSAGE_TYPE, type);
		cv.put(COLUMN_MESSAGE_DIRECTION, direction);
		cv.put(COLUMN_SEND_STATUS, sentStatus);
		cv.put(COLUMN_READ_STATUS, readStatus);
		return cv;
	}
	
	/**
	 * 自动生产消息id.
	 * @return
	 */
	public static String getAutoMessageid() {
		return String.valueOf(System.currentTimeMillis());
	}
}
